//-----------------------------------------------------------------------
// <copyright file="ResponsibleDAO.cs" company="Pyramid Consulting">
//     Copyright (c) Pyramid Consulting. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
	
using System;
using System.Data;
using MIMS.WRS.Entities;
using MyFramework;
using MyFramework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace MIMS.WRS.Data
{	
	public class ResponsibleDAO : ResponsibleDAOBase
	{
		public ResponsibleDAO()
		{
		}

		public virtual ResponsibleCollection GetResponsiblesByConPoint(int conPointID) 
		{
			int totalRecords = 0;
			return GetResponsiblesByConPoint(conPointID, ResponsibleColumns.Name, MyFramework.OrderDirection.ASC, 0, 0, out totalRecords);
		}

		public virtual ResponsibleCollection GetResponsiblesByConPoint(int conPointID, ResponsibleColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
		{            
			try
			{
				Database database = DatabaseFactory.CreateDatabase();
				DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spResponsiblesGetListByConPoint");
                	
				dbCommandWrapper.AddInParameter("@conPointID", DbType.Int32, conPointID);
				dbCommandWrapper.AddInParameter("@OrderBy", DbType.AnsiString, orderBy.ToString());
				dbCommandWrapper.AddInParameter("@OrderDirection", DbType.AnsiString, orderDirection.ToString());
				dbCommandWrapper.AddInParameter("@Page", DbType.Int32, page);
				dbCommandWrapper.AddInParameter("@PageSize", DbType.Int32, pageSize);
				dbCommandWrapper.AddOutParameter("@TotalRecords", DbType.Int32, 4);

				ResponsibleCollection responsibleCollection = new ResponsibleCollection();
				using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommandWrapper))
				{
					while (reader.Read())
					{
						Responsible responsible = CreateResponsibleFromReader(reader);

						 if (!reader.IsDBNull(reader.GetOrdinal("ConPoint"))) responsible.ConPoint = (string)reader["ConPoint"];
//						if (!reader.IsDBNull(reader.GetOrdinal("IsInUse"))) responsible.IsInUse = Convert.ToBoolean(reader["IsInUse"]);

						responsibleCollection.Add(responsible);
					}

					reader.Close();
				}
				
				totalRecords = (int)dbCommandWrapper.GetParameterValue("@TotalRecords");
				return responsibleCollection;
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new DataAccessException(SR.DataAccessGetResponsiblesException, ex);
			}
		}
	}
}
